<?php  
class ControllerCommonColumnLeftProduct extends Controller {
    protected function index() {
        $module_data = array();
        $this->load->model('catalog/attribute');
        $this->load->model('tool/seo_url');                                                      
        
        $path = isset($this->request->get['path']) ? $this->request->get['path'] : '0'; 
        $sort = isset($this->request->get['sort']) ? $this->request->get['sort'] : 'p.sort_order';
        $order = isset($this->request->get['order']) ? $this->request->get['order'] : 'ASC';
        $page = isset($this->request->get['page']) ? $this->request->get['page'] : 1;
        $limit = isset($this->request->get['limit']) ? $this->request->get['limit'] : 12; 
        $keyword = isset($this->request->get['keyword']) ? $this->request->get['keyword'] : '';
        
        $url = '&path=' . $path . '&sort=' . $sort . '&order=' . $order . '&limit=' . $limit;
        if($keyword) {
            $url .= '&keyword=' . $keyword;
        }

        $attribute_ids = array();
        if(isset($this->request->get['attribute']) && is_array($this->request->get['attribute'])){
            $attribute_ids = $this->request->get['attribute'];
        }
        $selected_attributes = array();   
        $attr_list = '';
        //foreach($attribute_ids as $key => $val){
        //$attribute_list = array();
        foreach($attribute_ids as $attribute_id){        
            foreach($attribute_id as $key => $val){ 
                //$attribute_list[] = $key;                
                $selected_attributes[] = $val;
                
                $attr_list .= '&attribute[][' . $key . ']=' . $val;
            }
        }
        //$attribute_list = array_unique($attribute_list);
        
        $parts = explode('_', $path);  
        $category_id = end($parts);
        //$attributes = $this->model_catalog_attribute->getAttributesByCategoryId($category_id, $order, $limit, $page);
        $attributes = $this->model_catalog_attribute->getAttributesByCategoryId($category_id, $order, 0, $page); 
        
        foreach($attributes as $key => &$attr_item){ 
            $filter_attr = false;                       
            foreach($attr_item as &$attr){                
                $attr['href'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category' . $url);
                $attr['reset_link'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category' . $url . $this->generateResetLink($key, $attributes, $selected_attributes));
                                
                foreach($attr['attribute_values'] as &$at){
                    if(in_array($at['value_id'], $selected_attributes)){
                        $at['selected'] = true;
                        $filter_attr = true;
                    }
                    else{
                        $at['selected'] = false;
                    }                    
                }
                
                $attr['filtered'] = $filter_attr;
            }              
        }      
        
        $this->data['filtered'] = ($attr_list != '');
        $this->data['attributes'] = $attributes;           
        $this->data['attributes_href'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category' . $url . $attr_list); 
        $this->data['attributes_reset'] = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category' . $url . '&page=' . $page); 
        
        $this->load->model('checkout/extension');
        $results = $this->model_checkout_extension->getExtensions('module');
        foreach ($results as $result) {
            if ($this->config->get($result['key'] . '_status') && ($this->config->get($result['key'] . '_position') == 'left')) {
                $module_data[] = array(
                    'code'       => $result['key'],
                    'sort_order' => $this->config->get($result['key'] . '_sort_order')
                );
                
                $this->children[] = 'module/' . $result['key'];
            }
        }
        
        $this->children[] = 'module/newsletter';
                
        $sort_order = array();       
        foreach ($module_data as $key => $value) {
              $sort_order[$key] = $value['sort_order'];
        }                      
        array_multisort($sort_order, SORT_ASC, $module_data);                    
        $this->data['modules'] = $module_data;
        
        $this->id = 'column_left_product';
        $this->data['template'] = $this->config->get('config_template');           

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/common/column_left_product.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/common/column_left_product.tpl';
        } else {
            $this->template = 'default/template/common/column_left.tpl';
        }
        
        $this->render();
    }
    
    private function generateResetLink($type, $attributes, $selected_attributes){
        $reset = '';
        foreach($attributes as $key => $attr_item){ 
            if($type != $key){                                    
                foreach($attr_item as $attr){
                    foreach($attr['attribute_values'] as $attr_value){
                        if(in_array($attr_value['value_id'], $selected_attributes)){
                            $reset .= '&attribute[][' . $attr['attribute_id'] . ']=' . $attr_value['value_id'];
                        }
                    }
                } 
            }             
        }
        
        return $reset;          
    }
}
?>